//
//  CZRetweetView.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/26.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
import SnapKit

class CZRetweetView: UIView {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        retweetPictureView.backgroundColor = UIColor.clearColor()
        prepareUI()
        
    }
    //用于记录转发数据模型的变量
    var retweetStatus:CZStatuses?{
        
        didSet{
            guard let name = retweetStatus?.user?.screen_name else{
              print("screen_name无值")
              return
            }
            guard let text = retweetStatus?.text else{
                print("text无值")
                return
            }
            
          retweetLable.text = "\(name)\(text)"
            //卸载原来的约束
            self.retweetViewBottomConstrain?.uninstall()
           //给图片赋值
            if let pictureURLs = retweetStatus?.pictureUrls where pictureURLs.count > 0 {
                //如果有值，则赋值，并显示
                retweetPictureView.hidden = false
                retweetPictureView.pictureUrls = pictureURLs
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewBottomConstrain = make.bottom.equalTo(retweetPictureView.snp_bottom).offset(margin).constraint
                })
                
            }else{
                
                retweetPictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewBottomConstrain = make.bottom.equalTo(retweetLable.snp_bottom).offset(margin).constraint
                })
                
                
            }
            
        }
        
    }
    
    //用于记录retweetView的大小约束对象
    private var retweetViewBottomConstrain: Constraint?

    //添加子控件和约束
    func prepareUI(){
        //添加子控件
        addSubview(retweetButton)
        addSubview(retweetLable)
        addSubview(retweetPictureView)
        //添加约束，button
        retweetButton.snp_makeConstraints { (make) -> Void in
            //四边对齐
            make.edges.equalTo(self)
        }
        //约束 lable
        retweetLable.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(margin)
            make.trailing.equalTo(self).offset(-margin)

        }
        //约束 retweetView
        retweetPictureView.snp_makeConstraints { (make) -> Void in
           
            make.top.equalTo(retweetLable.snp_bottom)
            make.leading.equalTo(self).offset(margin)
            make.size.equalTo(CGSizeZero)
        }
        //自身的底部约束
        self.snp_makeConstraints { (make) -> Void in
            //暂时当做没有图片在模型赋值时重新更新
            self.retweetViewBottomConstrain = make.bottom.equalTo(retweetPictureView.snp_bottom).constraint
        }
        
        
    }
    //覆盖整个转发view的button
    private lazy var retweetButton:UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        
        return button
    }()
    
    //转发的内容
    private lazy var retweetLable:UILabel = {
        let lable = UILabel()
        lable.numberOfLines = 0
        lable.font = UIFont.systemFontOfSize(12)
        lable.textColor = UIColor.lightGrayColor()
        lable.sizeToFit()
        lable.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * margin
        return lable
    }()
    //转发的配图,利用原创视图的统一布局即可
    private lazy var retweetPictureView:CZOriginalPictureView = CZOriginalPictureView()

}
